package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.GoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;

/**
 * Created by ${wushenglu} on 17/08/25.
 */
@Controller
@RequestMapping(ControllerConstants.COMMENT)
public class GoodsComentMCtronller extends BaseMobileController {
    @Autowired
    private GoodsCommentService goodsCommentService;

    /**
     * 添加评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addGoodsComment")
    public void addGoodsComment(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String orderNumber = getString(json, "orderNumber", "");
                int userId = getInt(json, "userId", 0);
                int bussId = getInt(json, "bussId", 0);
                String commentContent = getString(json, "commentContent", "[{\"id\":1,\"content\":\"45\",\"picture\":\"\",\"spec\":\"\",\"score\":5}]");
                return goodsCommentService.addGoodsComment(orderNumber, userId, bussId, commentContent);
            }
        });
    }

    /**
     * 查询我的所有评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findCommentByUserId")
    public void findCommentByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer nowPage = getInt(json, "nowPage", 0);
                Integer pageSize = getInt(json, "pageSize", 20);
                return goodsCommentService.findCommentByUserId(userId, nowPage, pageSize);
            }
        });
    }

    /**
     * 删除我的单个评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteCommentById")
    public void deleteCommentById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer commentId = getInt(json, "commentId", 0);
                return goodsCommentService.deleteCommentById(commentId);
            }
        });
    }

    /**
     * 添加追评
     *
     * @param request
     * @param response
     */
    @RequestMapping("/review")
    public void review(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer commentId = getInt(json, "commentId", 0);
                String review = getString(json, "review", "");
                String reviewImg = getString(json, "reviewImg", "");
                return goodsCommentService.review(commentId, review,reviewImg);
            }
        });
    }
}
